﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using LuxDiner.Model.Orders;
using LuxDiner.Logic.Orders;
namespace LuxDiner.Web.Management.Areas.Orders.Controllers
{
    [RouteArea("Orders")]
    [RoutePrefix("PaidOrder")]
    
    public class PaidOrderController : Controller
    {
        [Route("")]
        [Authorize(Roles = "Admin,PaidOrderList")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("NewOrder/{method:int}")]
        [Authorize(Roles = "Admin,PaidOrder")]
        public ActionResult NewOrder(int method)
        {
            ViewBag.Method = method;
            return View();
        }

        [Route("Order/{orderNo}")]
        public ActionResult Order(string orderNo)
        {
            PaidOrderModel model = null;
            if (!string.IsNullOrEmpty(orderNo))
                model = PaidOrderLogic.GetOrder(orderNo);
            return PartialView(model);
        }

        [Route("Print/{orderNo}")]
        public ActionResult Print(string orderNo)
        {
            PaidOrderModel model = null;
            if (!string.IsNullOrEmpty(orderNo))
                model = PaidOrderLogic.GetOrder(orderNo);
            return View(model);
        }

        [Route("Export")]
        public ActionResult Export()
        {
            try
            {
                PaidOrderQuery sea = new PaidOrderQuery();
                sea.GetCustomer = true;
                if (!string.IsNullOrEmpty(Request.Form.Get("customerID")))
                    sea.CustomerID = int.Parse(Request.Form.Get("customerID"));
                sea.OrderNo = Request.Form.Get("orderNo");
                var odb = Request.Form.Get("orderDateBefore");
                if (!string.IsNullOrEmpty(odb))
                    sea.OrderDateBefore = DateTime.Parse(odb);
                var oda = Request.Form.Get("orderDateAfter");
                if (!string.IsNullOrEmpty(oda))
                    sea.OrderDateAfter = DateTime.Parse(oda);
                var orderMethod = Request.Form.Get("orderMethod");
                if (!string.IsNullOrEmpty(orderMethod))
                    sea.Method = int.Parse(orderMethod);
                var lst = PaidOrderLogic.GetOrders(sea);
                if (lst.Count > 0)
                {
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(Server.MapPath("/RootFiles/Templates/Exports/paidorders.xls"));
                    var cells = workbook.Worksheets[0].Cells;
                    for (var i = 0; i < lst.Count; i++)
                    {
                        var item = lst[i];
                        cells[i + 1, 0].Value = item.OrderNo;
                        cells[i + 1, 1].Value = item.Customer.ComplexName;
                        cells[i + 1, 2].Value = item.NoteDate.ToString("yyyy-MM-dd");
                        cells[i + 1, 3].Value = Common.GetOrderName(item.Method);
                        cells[i + 1, 4].Value = item.TotalMoney;
                        cells[i + 1, 5].Value = item.Description;
                    }
                    var buffer = workbook.SaveToStream().ToArray();
                    return File(buffer, "application/octet-stream", "账务单据明细.xls");
                }
                else
                    return Content("暂无数据");
            }
            catch (Exception ex)
            {
                Common.Logger.Error("", ex);
                return Content("服务器运行出错，请联系开发商。");
            }
        }
    }
}
